package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.reconciliation.FindReconciliationReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationKaipingReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.mapper.KaipingGuyMapper;
import com.roc.steelend.mapper.KaipingOrderMapper;
import com.roc.steelend.mapper.ReconciliationKaipingMapper;
import com.roc.steelend.pojo.KaipingGuy;
import com.roc.steelend.pojo.ReconciliationKaiping;
import com.roc.steelend.pojo.ReconciliationSale;
import com.roc.steelend.service.ReconciliationKaipingService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.annotation.WebServlet;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReconciliationKaipingServiceImpl implements ReconciliationKaipingService {


    @Autowired
    private ReconciliationKaipingMapper reconciliationKaipingMapper;

    @Autowired
    private KaipingGuyMapper kaipingGuyMapper;
    @Autowired
    private KaipingOrderMapper kaipingOrderMapper;

    @Override
    public ReturnMsg postReconciliationKaiping(ReconciliationKaipingReception reconciliationKaipingReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
//        生成调账数据
        ReconciliationKaiping reconciliationKaiping = new ReconciliationKaiping();
        BigInteger reconciliationKaipingLsh = LshUtil.getReconciliationKaiping();
        reconciliationKaiping.setReconciliationKaipingId(reconciliationKaipingLsh);
        reconciliationKaiping.setReconciliationKaipingTime(reconciliationKaipingReception.getReconciliationTime());
        reconciliationKaiping.setOperateTime(new Timestamp(new Date().getTime()));
        reconciliationKaiping.setKaipingguyId(reconciliationKaipingReception.getKaipingguyId());
        reconciliationKaiping.setEmployeeId(employeeId);
        if (Objects.equals(reconciliationKaipingReception.getRemark(), "")){
            reconciliationKaipingReception.setRemark(null);
        }
        reconciliationKaiping.setRemark(reconciliationKaipingReception.getRemark());
        reconciliationKaiping.setOriginallyMoney(reconciliationKaipingReception.getOldDebt());
        reconciliationKaiping.setNowMoney(reconciliationKaipingReception.getNewDebt());
        int insert = reconciliationKaipingMapper.insert(reconciliationKaiping);
        if (insert!=1){
                msg.setCode(0);
                msg.setMsg("调账操作失败");
                return msg;
        }

        QueryWrapper<KaipingGuy> kaipingGuyQueryWrapper = new QueryWrapper<>();
        kaipingGuyQueryWrapper.eq("kaipingguy_id",reconciliationKaipingReception.getKaipingguyId());
        KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingGuyQueryWrapper).get(0);
        kaipingGuy.setDebt(reconciliationKaipingReception.getNewDebt());
        kaipingGuyMapper.updateById(kaipingGuy);

        kaipingOrderMapper.updateReconciliationByKaipingGuy(reconciliationKaiping.getKaipingguyId(), reconciliationKaipingLsh);
        msg.setMsg("调账操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findReconciliationDataByKaiping(FindReconciliationReception findReconciliationReception) {
        Integer now = findReconciliationReception.getCurrentPage();
        Integer size = findReconciliationReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findReconciliationReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return reconciliationKaipingMapper.findReconciliationDataByKaiping(now,size,duiXiang);
    }

    @Override
    public Integer findReconciliationDataByKaipingCount(FindReconciliationReception findReconciliationReception) {
        String duiXiang = findReconciliationReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return reconciliationKaipingMapper.findReconciliationDataByKaipingCount(duiXiang);
    }

    @Override
    public List<ReconciliationKaiping> kaipingReconciliationDetailSelect(BigInteger reconciliationId) {

        QueryWrapper<ReconciliationKaiping> reconciliationKaipingQueryWrapper = new QueryWrapper<>();
        reconciliationKaipingQueryWrapper.eq("reconciliation_kaiping_id",reconciliationId);
        return reconciliationKaipingMapper.selectList(reconciliationKaipingQueryWrapper);
    }
}
